package com.xuan.yupao.controller;
import java.io.InputStream;

import com.xuan.yupao.common.ErrorCode;
import com.xuan.yupao.exception.BusinessException;


import com.xuan.yupao.common.Result;
import com.xuan.yupao.model.domain.User;
import com.xuan.yupao.service.FileService;
import com.xuan.yupao.service.UserService;
import com.xuan.yupao.utils.FileUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/file")
public class FileController {



    @Resource
    private UserService userService;

    @Resource
    private FileService fileService;

    @PostMapping("/user/upload")
    public Result<String> uploadUserAvatar(MultipartFile file, HttpServletRequest request) {
        if (file == null || file.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件为空");
        }
        User loginUser = userService.getLoginUser(request);

        String result = fileService.uploadUserAvatar(file, loginUser);

        return Result.success(result);
    }

    @PostMapping("/team/upload/{teamId}")
    public Result<String> uploadTeamAvatar(MultipartFile file, HttpServletRequest request, @PathVariable(value = "teamId") Long teamId) {
        if (file == null || file.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件为空");
        }
        if (teamId == null || teamId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍参数为空");
        }
        User loginUser = userService.getLoginUser(request);

        String result = fileService.uploadTeamAvatar(file, loginUser, teamId);
        return Result.success(result);
    }
}
